package com.alpha.rpc;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.test.AbstractTransactionalDataSourceSpringContextTests;

public abstract class BaseManagerTestCase extends AbstractTransactionalDataSourceSpringContextTests {
	//~ Static fields/initializers =============================================

	protected final Log log = LogFactory.getLog(getClass());
	protected static ResourceBundle rb = null;

	protected String[] getConfigLocations() {
		setAutowireMode(AUTOWIRE_BY_NAME);
		return new String[] { "classpath*:applicationContext-test.xml", "classpath*:spring/applicationContext-*.xml" // for web projects
		};
	}

	public BaseManagerTestCase() {
		// Since a ResourceBundle is not required for each class, just
		// do a simple check to see if one exists
		String className = this.getClass().getName();

		try {
			rb = ResourceBundle.getBundle(className);
		} catch (MissingResourceException mre) {
			//log.warn("No resource bundle found for: " + className);
		}
	}

	protected Object populate(Object obj) throws Exception {
		// loop through all the beans methods and set its properties from its .properties file
		Map<String, String> map = new HashMap<String, String>();

		for (Enumeration<String> keys = rb.getKeys(); keys.hasMoreElements();) {
			String key = keys.nextElement();
			map.put(key, rb.getString(key));
		}

		BeanUtils.copyProperties(map, obj);

		return obj;
	}
}